package com.cet.designpatterns.state;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @program: algorithm
 * @description: 可以抽奖的状态
 * @author: 陈恩涛
 * @create: 2021-09-26 22:33
 **/
public class CanRaffleState extends State{

    private RaffleActivity activity;

    public CanRaffleState(RaffleActivity activity) {
        this.activity = activity;
    }

    @Override
    public void deductPoints() {
        System.out.println("已经扣除过积分了");
    }

    @Override
    public boolean raffle() {
        System.out.println("正在抽奖，请稍等......");
        SecureRandom sr = null;
        try {
            sr = SecureRandom.getInstanceStrong(); // 获取高强度安全随机数生成器
        } catch (NoSuchAlgorithmException e) {
            sr = new SecureRandom(); // 获取普通的安全随机数生成器
        }
        int num = sr.nextInt(10);
        if (num == 0) {
            activity.setState(activity.getDispenseState());
            return true;
        }
        System.out.println("很遗憾没有抽中奖品！");
        activity.setState(activity.getNoRafflleState());
        return false;
    }

    @Override
    public void dispensePrize() {
        System.out.println("还未中奖，请进行抽奖！");
    }
}
